import localforage from 'localforage'

const COMMON = 'common:'
const IP_KEY = COMMON + 'ip_key'

const state = {
  ip: 'http://10.156.8.50',
  // ip: '',
  navigation: '',
  username: '',
  userid: '',

}
const getters = {
  ip: state => state.ip,
  navigation: state => state.navigation,
}

const mutations = {
  SET_IP(state, ip) {
    state.ip = "http://" + ip
  },
  SET_NAVIGATION(state, nav) {
    state.navigation = nav
  }
}
const actions = {
  loadIP({ commit }) {
    return new Promise((resolve, reject) => {
      localforage.getItem(IP_KEY).then(val => {
        if (val) {
          resolve(commit('SET_IP', val))
        } else {
          reject('unset')
        }
      }).catch(e => {
        reject(e)
      })
    })
  },
  setIP({ commit }, ip) {
    return new Promise((resolve, reject) => {
      localforage.setItem(IP_KEY, ip).then(() => {
        resolve(commit('SET_IP', ip))
      }).catch(e => {
        reject(e)
      })
    })
  }
}

export default {
  state,
  getters,
  mutations,
  actions
}
